package fr.waitandsee.fragments;

import fr.waitandsee.FragmentController;
import fr.waitandsee.R;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SignInFragment extends Fragment implements PageFragmentModel {
	
	private static final String TAG = "SignInFragment";
	
	private FragmentController fragmentController;
	private Handler handler;
	
	private EditText etUsername;
	private EditText etPassword;
	private Button bEnter;
	private Button bSignUp;
	private TextView tvForgottenPwd;
	
	public static SignInFragment newInstance(/*parameters*/) {
		SignInFragment fragment = new SignInFragment();
		// set parameters if needed : 
	    // Bundle bundle = new Bundle(1);
	    // bundle.putInt(EXTRA_NAME, param);
	    // fragment.setArguments(bundle);
	    return fragment ;
	}
	
	
	  /////////////////////////
	 // FRAGMENT LIFE CYCLE //
	/////////////////////////
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate.savedInstanceState="+savedInstanceState);
		this.fragmentController = (FragmentController) getActivity();
		this.handler = new Handler();
	}
	
 	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 		Log.d(TAG, "onCreateView.savedInstanceState="+savedInstanceState);
		if (savedInstanceState != null) {
			// get saved parameters if needed : 
			// param = savedInstanceState.getInt(EXTRA_NAME);
		} else {
			if(getArguments() != null) {
				// get parameters if needed :
				// param = getArguments().getInt(EXTRA_NAME);
			}
		}
		View v = inflater.inflate(R.layout.fragment_signin, container, false);
		return v;
	}
	
 	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
 		Log.d(TAG, "onViewCreated.savedInstanceState="+savedInstanceState);
		fetchViews();
		customizeViews();
		setListeners();
	}
 	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		Log.d(TAG, "onSaveInstanceState.outState="+outState);
		super.onSaveInstanceState(outState);
		// save parameters if needed :
		// outState.putInt(EXTRA_NAME, param);
	}
 	
	@Override
 	public void fetchViews() {
 		this.etUsername = (EditText) this.getView().findViewById(R.id.editText_login);
 		this.etPassword = (EditText) this.getView().findViewById(R.id.editText_password);
 		this.bEnter = (Button) this.getView().findViewById(R.id.button_signIn);
 		this.bSignUp = (Button) this.getView().findViewById(R.id.button_signUp);
 		this.tvForgottenPwd = (TextView) this.getView().findViewById(R.id.textView_forgotten_pwd);
 	}
	
	@Override
	public void customizeViews() {
		
	}
 	
	@Override
	public void setListeners() {
 		this.bEnter.setOnClickListener(onEnterClickListener);
 		this.bSignUp.setOnClickListener(onSignupClickListener);
 		this.tvForgottenPwd.setOnTouchListener(onForgottenPwdTouchListener);
 	}
	
	
	  ///////////////
	 // LISTENERS //
	///////////////
 	
 	private OnClickListener onEnterClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Log.i(TAG, "onEnterClickListener");
			//TODO action on "Enter" button clicked
			String username = etUsername.getText().toString(); 			//DEBUG
			String password = etPassword.getText().toString(); 			//DEBUG
			Log.i(TAG, "username="+username+", password="+password);	//DEBUG
			//TODO Switch to "HomeFragment" after action performed :
			//fragmentController.switchContent(HomeFragment.newInstance());
		}
	};
	
 	private OnClickListener onSignupClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Log.i(TAG, "onSignupClickListener");
			fragmentController.switchContent(SignUpFragment.newInstance());
		}
	};
	
	private OnTouchListener onForgottenPwdTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			Log.i(TAG, "onForgottenPwdTouchListener");
			tvForgottenPwd.setOnTouchListener(null); // stand-by listener to prevent multi openning
			handler.postDelayed(delayAfterTouchCallback, 200); // re-set on touch listener after 200ms
			//TODO action on "forgotten password" textview touched
			return false;
		}
 	};
 	
 	private Runnable delayAfterTouchCallback = new Runnable() {
 		@Override 
 		public void run() {
 			tvForgottenPwd.setOnTouchListener(onForgottenPwdTouchListener);
 		}
 	};
	
	
}
